** load data and set missings
** Need to download these files from the Gesis/GLES site https://www.gesis.org/en/elections-home/gles
use ZA6802_de_v3-0-1.dta
do ZA6802_de_missing.do
* Construct dependent variable use information Wahl-Variable
* pre/post election (ballot box) + postal votes (only pre-election)
* also define labels

    * Only keep voters (18 years or older = born before 2000)
    drop if vn2c >1999
    drop if vn2d == 0

    gen wahl =.
    lab define wahl 0 "other" 1 "Left" 2 "AfD"
    lab val wahl wahl 

    * Briefwahl rekodieren 
    replace wahl = 0 if v12ba ==1   // CDU
    replace wahl = 0 if v12ba ==4   // SPD
    replace wahl = 0 if v12ba ==5   // FDP
    replace wahl = 0 if v12ba ==6   // Grüne
    replace wahl = 0 if v12ba ==801 // Andere
    replace wahl = 1 if v12ba ==7   // Linke
    replace wahl = 2 if v12ba ==322 // AfD     

    * pre-election: ballot

    replace wahl = 0 if v11ba ==1  
    replace wahl = 0 if v11ba ==4  
    replace wahl = 0 if v11ba ==5  
    replace wahl = 0 if v11ba ==6  
    replace wahl = 0 if v11ba ==801
    replace wahl = 1 if v11ba ==7  
    replace wahl = 2 if v11ba ==322

    * post-election: ballot 
    replace wahl = 0 if n11ba ==1  
    replace wahl = 0 if n11ba ==4  
    replace wahl = 0 if n11ba ==5  
    replace wahl = 0 if n11ba ==6  
    replace wahl = 0 if n11ba ==801
    replace wahl = 1 if n11ba ==7  
    replace wahl = 2 if n11ba ==322

    * Age
    gen alter = 2017 - vn2c
    recode alter (18/34 =1 "18-34") (35/49=2 "35-49") (50/65=3 "50-65") (65/100 = 4 "65+"), gen(catalter)
    lab var catalter "Age (cat.)"
    recode vn1 (2=0 "female") (1=1 "male"), gen(gender)
    lab var gender "gender"

    * formal education
    recode vn136 (1/2=1 "low") (3=2 "medium") (4/9=3 "high"), gen(education)
    lab var education "formal education"

    * Region: east vs west (special treatment of Berlin)

    recode ostwest2 (0=1 "East") (1=0 "West") , gen(east)
    lab var east "Region Interview"

    * party identification 
    recode vn99a (1/6 =0 "other") (7=1 "Linke") (322=2 "AfD") (801=0) (808=4 "none") (.a=4) (.b=4), gen(pid)
    label var pid "party ID"

    * Federal district 75 Berlin-Mitte on the former border is assigned to east, because  this is where most respondents live 
    gen eastnew = east
    replace eastnew = 0 if wahlkreis == 75

    * Complete model
    gsem (1.wahl <- i.education i.gender i.catalter vn59 vn60 vn6 i.eastnew W1[eastnew > wahlkreis] ) (2.wahl <- i.education i.gender i.catalter vn59 vn60 vn6 i.eastnew   W2[eastnew > wahlkreis] ) [pw=vn_w_ipfges] , link(logit) family(multinomial)

  est store komplett 
  gen imsample = e(sample)

   * reduced model 
  gsem (1.wahl <- i.education i.gender i.catalter i.eastnew W1[eastnew > wahlkreis] ) (2.wahl <- i.education i.gender i.catalter i.eastnew   W2[eastnew > wahlkreis] ) [pw=vn_w_ipfges] if imsample , link(logit) family(multinomial)
  est store wahlredux


   * model for economic dimension
  gsem (vn59 <- i.education i.gender i.catalter i.eastnew W1[eastnew > wahlkreis] )  [pw=vn_w_ipfges] if imsample
  est store econ

  * model for socio-cultural dimension (immigration)
  gsem (vn60 <- i.education i.gender i.catalter i.eastnew W1[eastnew > wahlkreis] )  [pw=vn_w_ipfges] if imsample
  est store zuwan

  * Modell disaffection 
  gsem (vn6 <- i.education i.gender i.catalter i.eastnew W1[eastnew > wahlkreis] )  [pw=vn_w_ipfges] if imsample
  est store unzuf

  * fehlende Labels 

  lab val eastnew eastnew
  lab def eastnew 0 "West" 1 "East"






  * Rename option does not work with gsem 
  * Use this solution:
  * https://www.stata.com/statalist/archive/2013-01/msg01021.html

  * Copy matrix and read names, then change them

  program changename, eclass

    matrix betass=e(b)
    local names : colfullnames e(b)

    local names: subinstr local names "e.vn6)" "e.vn59)" 
    local names: subinstr local names "e.vn60)" "e.vn59)" 
    matrix colnames betass = `names'
    ereturn repost b = betass, rename 
  end  

  est restore zuwan
  changename
  est store zuwan

  est restore unzuf
  changename
  est store unzuf

  * write first table as LaTeX

  estout econ zuwan unzuf using multi-level-1.tex , cells((b(star fmt(3)) se(par))) label refcat(2.education Bildung: 1.gender Gender: 2.catalter Alter: 1.eastnew Befragungsgebiet:,  nolabel) drop(1.education 0.gender 1.catalter 0.eastnew W* ) eqlabels("Fixed" "Random") mlabels("Steuern/Soziales" "Zuwanderung" "Unzufrieden Demokratie",span prefix(\multicolumn{@span}{c}{) suffix(})) varlabels(N_clust N2 N N1 _cons Konstante var(W1[eastnew>wahlkreis]) "Var (Wahlkreis)" /:var(e.vn59) "Var (Person)" var(e.vn60) "Var (Person)" var(e.vn6) "Var (Person)") stat(N N_clust,fmt("%9.0f") label(N1 N2)) replace style(tex)


  

  * write second table as LateX
  estout wahlredux komplett using multi-level-2.tex  , cells((b(star fmt(3)) se(par))) label refcat(2.education Bildung: 1.gender Gender: 2.catalter Alter: 1.eastnew Befragungsgebiet:,  nolabel) drop(1.education 0.gender 1.catalter 0.eastnew 0b.wahl:_cons W*) eqlabels("Fixed: Linke" "Fixed AfD") mlabels("Wahlentscheidung (1)" "Wahlentscheidung (2)") varlabels(_cons Konstante vn59 "Steuern/Soziales" vn60 "Zuwanderung" vn6 "Unzufrieden Demokratie" var(W1[eastnew>wahlkreis]) "Var (Wahlkreis) Linke" var(W2[eastnew>wahlkreis]) "Var (Wahlkreis) AfD"  cov(W1[eastnew>wahlkreis],W2[eastnew>wahlkreis]) "Kovarianz Linke/AfD")  subst(/ "") replace style(tex)


  * Calculate margins and store

  * tax/welfare
  est restore komplett
  margins, at(vn59=(1 4 6 8 11) eastnew=(0 1)) post
  parmest , saving(oekmargins, replace)



  * immigration

  est restore komplett
  margins, at(vn60=(1 4 6 8 11) eastnew=(0 1)) post
  parmest , saving(migmargins, replace)  



